package app.fugema.shop.main.order.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.base.common.utils.LogUtil;

import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseViewHolder;
import com.jeremyliao.liveeventbus.LiveEventBus;

import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.ShopingConfirmOrderInvoiceBinding;
import app.fugema.shop.main.order.model.ConfirmAmountFooterBean;
import app.fugema.shop.main.setting.model.EventMsg;
import app.fugema.shop.util.MoneyUtils;
import app.fugema.shop.util.SimpleTextWatcher;

public class ShoppingConfirmInvoiceMultiType extends BaseItemMultiType<ConfirmAmountFooterBean, ShopingConfirmOrderInvoiceBinding> {

    @Override
    public int getItemViewType()
    {
        return TYPE_FOOT_ONE;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.shoping_confirm_order_invoice;
    }

    @Override
    public void onCreateViewHolder(ShopingConfirmOrderInvoiceBinding binding, BaseViewHolder viewHolder)
    {
        super.onCreateViewHolder(binding, viewHolder);
    }

    @Override
    public void onBindViewHolder(ShopingConfirmOrderInvoiceBinding binding, int position, ConfirmAmountFooterBean bean)
    {
        super.onBindViewHolder(binding, position, bean);

        //是否允许使用 积分余额
        if( bean.isCanIntegralPerMoney() )
        {
            binding.llIntegralView.setVisibility(View.VISIBLE);
        }
        else
        {
            binding.llIntegralView.setVisibility(View.GONE);
        }
        if( bean.isCanCapital() )
        {
            binding.rlWalletPayView.setVisibility(View.VISIBLE);
        }
        else
        {
            binding.rlWalletPayView.setVisibility(View.GONE);
        }

        //当 需要支付的钱 超过 余额时 不让用户 点击 余额支付
        //bean.getOrderTotalAmout() > bean.getCapitalAmount()
        if(  bean.isNotAllowWallet()  )
        {
            binding.btnWalletSwitch.setToggleOff();
            binding.btnWalletSwitch.setEnabled(false);
        }

        // 用来关闭 使用余额
        if(bean.isUseWallet())
        {
            binding.btnWalletSwitch.setToggleOn();
        }
        else
        {
            binding.btnWalletSwitch.setToggleOff();
        }

        //是否开启 余额支付
        binding.btnWalletSwitch.setOnToggleChanged(on -> {
            LogUtil.e("是否开启余额支付 -> " + on);
            LiveEventBus.get(AppConstants.EventKey.WALLET_PAY_SWITCH,Boolean.class).post(on);
        });

        binding.btnUseIntegralSwitch.setOnToggleChanged(on ->{
            if( on )
            {
                LogUtil.e("开启使用积分 -> " + on);
                binding.rlUseIntegralView.setVisibility(View.VISIBLE);
                setJifenToMoney(binding, bean);
            }
            else
            {
                LogUtil.e("关闭使用积分 -> " + on);
                binding.rlUseIntegralView.setVisibility(View.GONE);
            }
            //开启关闭 积分
            LiveEventBus.get(AppConstants.EventKey.CONFIRM_ORDER_CLICK, EventMsg.class).post( new EventMsg(1011, on ) );
        });

        //这里 积分直接 扣完 不让 用户填写

        //输入积分
        /*
        binding.etJifen.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(String content)
            {
                String currentFenStr = content;
                LogUtil.e("修改积分 jifen = " + currentFenStr );
                if(UIUtils.isNotEmpty( currentFenStr ))
                {
                    int jifen = Integer.parseInt( currentFenStr );
                    //输入的积分大于
                    if( jifen > bean.getTotalMemberIntegral() )
                    {

                        binding.etJifen.setText( String.valueOf( bean.getTotalMemberIntegral() ) );
                        bean.setIntegralPerMoney( bean.getUserIntegralMaxDeductible() );
                        binding.executePendingBindings();
                    }
                    else
                    {
                        // 抵扣的钱   300 积分 兑换 100 分(1元)
                        int fenMoney =  (int)((jifen * bean.getUserIntegralMaxRate()) / bean.getIntegralPerMoneyRate());
                        //使用的积分
                        bean.setUserIntegrals( jifen );
                        // 抵扣的钱
                        bean.setIntegralPerMoney(fenMoney*0.01f);
                        binding.executePendingBindings();
                    }
                }
                else
                {
                    //0 积分时
                    //使用的积分
                    bean.setUserIntegrals( 0 );
                    // 抵扣的钱
                    bean.setIntegralPerMoney(0);
                    binding.executePendingBindings();
                }
                //开启关闭 积分
                LiveEventBus.get(AppConstants.EventKey.CONFIRM_ORDER_CLICK, EventMsg.class).post( new EventMsg(1012, bean ) );
            }
        });
        */
    }

    @Override
    public void onItemClick(View view, int onclickType, int position, ConfirmAmountFooterBean bean)
    {
        super.onItemClick(view, onclickType, position, bean);
    }

    private void setJifenToMoney(ShopingConfirmOrderInvoiceBinding binding ,ConfirmAmountFooterBean bean )
    {
        //binding.tvOrderPayMoney.setText( String.valueOf( bean.getOrderTotalAmout() ) );
        binding.etJifen.setText( String.valueOf( bean.getUserIntegrals() ) );
        binding.tvJifenToMoney.setText( new StringBuffer().append("-¥").append(bean.getIntegralPerMoney()).toString()  );
    }

}
